package org.example.dao;

import org.example.pojo.Comment;
import org.example.pojo.User;
import org.example.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

// 评论
public class CommentDao {
    public List<Comment> getComment(int publishId) throws SQLException {
        //		获得数据库链接
        Connection connection = JDBCUtils.getConnection();
//        String sql = "insert into users (username,password) values(?,?)";
        String sql = "select * from comment where publish_id=?";
//		使用PreparedStatement执行SQL语句可以防止SQL注入
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,publishId);
        ResultSet rs = ps.executeQuery();
//		将结果集封装到集合
        List<Comment> list = new ArrayList<>();
        while (rs.next()) {
            Comment comment=new Comment(rs.getInt("id"),rs.getString("username"),rs.getString("content"),rs.getInt("user_id"),rs.getInt("publish_id"),rs.getDate("create_time"),rs.getDate("update_time"));
            list.add(comment);
        }
        return list;
    }

    //添加评论
    public boolean insertComment(int userId,int publishId,String username,String content) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "insert into comment(username,content,update_time,user_id,publish_id,create_time) values(?,?,now(),?,?,now());";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2,content);
        ps.setInt(3,userId);
        ps.setInt(4,publishId);
        return ps.executeUpdate()>0;
    }

    // 统计 用户的 评论数
    public int countComment(int userId) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select count(*) from comment where user_id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userId);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        }
        return 0;
    }

    // 更具用户 删除 评论
    public boolean deleteComment(int choose) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "delete from comment where id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,choose);
        return ps.executeUpdate()>0;
    }

    // 更具 评论id 获取 用户
    public String getUsername(int commentId) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "select username from comment where id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,commentId);
        ResultSet rs = ps.executeQuery();
//        User user = new User();
        if (rs.next()) {
//            System.out.println(rs.getString("username"));
            return rs.getString("username");
        }
        return null;
    }

    // 通过用户 和 评论id 删除评论
    public boolean deleteComment(int userId,int commentId) throws SQLException {
        Connection connection = JDBCUtils.getConnection();
        String sql = "delete from comment where user_id=? and id=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,userId);
        ps.setInt(2,commentId);
        return ps.executeUpdate()>0;
    }


}
